package br.gen.seam.util;

import br.gen.seam.util.exception.UtilException;

/**
 *
 * @author Diego Araujo | diegogomesaraujo@gmail.com
 */
public class StringUtil {

    public static String upperFirstCharacter(String str) throws UtilException {
        checkString(str);

        String firstCharacter = String.valueOf(str.charAt(0));
        str = str.substring(1);
        return firstCharacter.toUpperCase() + str;
    }

    public static String lowerFirstCharacter(String str) {
        try {
            checkString(str);
            String firstCharacter = String.valueOf(str.charAt(0));
            str = str.substring(1);
            return firstCharacter.toLowerCase() + str;
        } catch (UtilException e) {
            return "";
        }
    }

    public static String lowerFirstCharacter(Object str) {
        return lowerFirstCharacter(String.valueOf(str));
    }

    public static String convertToPatternClassName(String str, String regex) throws UtilException {
        checkString(str);

        String[] tokens = str.split(regex);
        str = "";
        for (String s : tokens) {
            str += StringUtil.upperFirstCharacter(s);
        }
        return str;
    }

    public static String convertToPatternAttributeName(String str, String regex) throws UtilException {
        checkString(str);

        String[] tokens = str.split(regex);
        str = "";
        String s = null;
        for (int i = 0; i < tokens.length; i++) {
            s = tokens[i];
            if (i == 0) {
                str += StringUtil.lowerFirstCharacter(s);
            } else {
                str += StringUtil.upperFirstCharacter(s);
            }
        }
        return str;
    }

    private static void checkString(String str) throws UtilException {
        if (str == null || str.length() == 0) {
            throw new UtilException("The string is not valid!");
        }
    }

    public static String join(String[] aStr, String separator) {
        String str = "";
        for (int i = 0; i < aStr.length; i++) {
            if (i == 0) {
                str += aStr[i];
            } else {
                str += separator + aStr[i];
            }
        }
        return str;
    }
}
